package com.leanyuan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.publi.domain.Building;
import com.leanyuan.publi.domain.TParkinglot;
import com.leanyuan.publi.service.BuildingService;
import com.leanyuan.publi.service.TParkinglotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 相亲相爱一家人
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/leanyuan/t-parkinglot")
public class TParkinglotController {
    //停车场的Service
    @Autowired
    TParkinglotService tParkinglotService;

    //楼栋信息的Service
    @Autowired
    BuildingService buildingService;

    //查询所有（分页显示）
    @RequestMapping("selectPageParkinglot")
    public IPage<TParkinglot> selectPageParkinglot(Integer page, Integer pageSize, Model model){
        IPage<TParkinglot> iPage=new Page<TParkinglot>(page,pageSize);
        IPage<TParkinglot> tParkinglotIPage= this.tParkinglotService.page(iPage,null);
        model.addAttribute("tParkinglotIPage",tParkinglotIPage);
        return tParkinglotIPage;
    }


    //查询楼栋信息
    @RequestMapping("selectBuilDingAll")
    public List selectBuilDingAll(Model model){
        List<Building> buildings=this.buildingService.list(null);
        model.addAttribute("buildings",buildings);
        return buildings;
    }

    //添加停车场
    @RequestMapping( "addParkingLot")
    public boolean saveParkinglot(@RequestBody TParkinglot tParkinglot){
        QueryWrapper<TParkinglot> queryWrapper =new QueryWrapper<TParkinglot>();
        boolean bole= this.tParkinglotService.save(tParkinglot);
        queryWrapper.eq("building_id",tParkinglot.getBuildingId());
        List<TParkinglot> Parkinglot1 =this.tParkinglotService.list(queryWrapper);
        if(Parkinglot1.size()>=2){
            this.tParkinglotService.removeById(tParkinglot.getParkinglotId());
            return  false;
        }
        return bole;
    }

    //根据ID删除停车场
    @RequestMapping( "deleteParkingLotById")
    public boolean deleteParkingLotById(Integer id){
        return this.tParkinglotService.removeById(id);
    }

    //根据id修改信息
    @RequestMapping("updateParkingLotById")
    public boolean updateParkingLotById(@RequestBody TParkinglot tParkinglot){
        QueryWrapper<TParkinglot> queryWrapper =new QueryWrapper<TParkinglot>();
        queryWrapper.eq("building_id",tParkinglot.getBuildingId());

        QueryWrapper<TParkinglot> queryWrapper1 =new QueryWrapper<TParkinglot>();
        queryWrapper1.eq("parkinglot_id",tParkinglot.getParkinglotId());

        TParkinglot tParkinglot2=this.tParkinglotService.getOne(queryWrapper1);

        boolean bole= this.tParkinglotService.updateById(tParkinglot);

        List<TParkinglot> Parkinglot1 =this.tParkinglotService.list(queryWrapper);
        if(Parkinglot1.size()>=2){
            this.tParkinglotService.updateById(tParkinglot2);
            return  false;
        }
        return bole;
    }

    //带条件的查询分页
    @RequestMapping("selectPageParkinglotByParam")
    public IPage<TParkinglot> selectPageParkinglotByParam(String parkinglotname, String cartype, Integer parkinglotid, Integer page, Integer pageSize, Model model){
        //分页
        IPage<TParkinglot> iPage=new Page<TParkinglot>(page,pageSize);
        //条件
        QueryWrapper<TParkinglot> queryWrapper=new QueryWrapper<TParkinglot>();
        if(parkinglotname !="" && parkinglotname !=null) {
            queryWrapper.eq("building_id", parkinglotname);
        }if(cartype !="" && cartype !=null) {
            queryWrapper.eq("parkinglot_type", cartype);
        }if(parkinglotid >=0){
            queryWrapper.eq("parkinglot_id",parkinglotid);
        }

        IPage<TParkinglot> tParkinglotIPage= this.tParkinglotService.page(iPage,queryWrapper);
        model.addAttribute("tParkinglotIPageParam",tParkinglotIPage);
        return tParkinglotIPage;
    }
}

